package nba2kmodtool.util;

public abstract class H7CompressUtils {

	private static int composeBits(int H, int HU, int HL) {
		return ((H >> HL) & ((((int) 1) << ((HU - HL) + 1)) - 1));
	}

	/**
	 * 解压普通压缩数据
	 * @param src
	 * @param decompressSize
	 * @param hL
	 * @return
	 */
	public static byte[] decompress(byte[] src, int decompressSize, int hL) {
		byte[] outbuf = new byte[(int) decompressSize];
		int datalen = src.length;
		int numbput = 0;
		int numbget = 0;
		while (numbput < datalen) {
			int numb5 = src[numbput++] & 0xff;
			int numb6 = 0;
			do {
				if (numbget == decompressSize) {
					break;
				}
				if ((numb5 & 1) > 0) {
					int a = src[numbput++] & 0xff;
					int b = src[numbput++] & 0xff;
					int h = a * 0x100 + b;
					int numb7 = composeBits(h, 15, hL) + 3;
					int numb8 = composeBits(h, hL - 1, 0);
					int numb11 = numb7 - 1;
					for (int j = 0; j <= numb11; j++) {
						if (((numbget - numb8) < 0) || (numb8 == 0)) {
							outbuf[numbget++] = (byte) 0xff;
						} else {
							outbuf[numbget] = outbuf[numbget - numb8];
							numbget++;
						}
					}
				} else {
					outbuf[numbget++] = src[numbput++];
				}
				numb5 = numb5 >> 1;
				numb6++;
			} while (numb6 <= 7);
		}
		return outbuf;
	}

}
